#include "MediaPlayerVideo.h"
#include "ui_MediaPlayerVideo.h"
#include <QDir>
#include <QFileDialog>
#include <QUrl>

MediaPlayerVideo::MediaPlayerVideo(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MediaPlayerVideo)
{
    ui->setupUi(this);
    m_Player = new QMediaPlayer (this) ;//创建视频播放器
    m_Player->setNotifyInterval (2000) ;//信息更新周期2000 ms
    m_Player->setVideoOutput (ui->videoWidget) ; //视频显示组件

    ui->btnPlay->setEnabled(true);
    ui->btnPause->setEnabled(false);
    ui->btnStop->setEnabled(false);

    connect (m_Player, &QMediaPlayer::stateChanged,this, &MediaPlayerVideo::onStateChanged) ;
    connect (m_Player, &QMediaPlayer::positionChanged , this, &MediaPlayerVideo::onPositionChanged) ;
    connect (m_Player, &QMediaPlayer::durationChanged,this, &MediaPlayerVideo::onDurationChanged) ;
}

MediaPlayerVideo::~MediaPlayerVideo()
{
    delete ui;
}

void MediaPlayerVideo::onStateChanged(QMediaPlayer::State state)
{
    //播放器状态变化
    ui->btnPlay->setEnabled(!(state==QMediaPlayer::PlayingState)) ;
    ui ->btnPause->setEnabled (state==QMediaPlayer::PlayingState) ;
    ui ->btnStop->setEnabled (state==QMediaPlayer::PlayingState) ;
}

void MediaPlayerVideo::onDurationChanged(qint64 duration)
{
    //文件时长变化
    ui->sliderPosition->setMaximum (duration) ;

    int secs=duration/1000; //秒
    int mins=secs/60; //分钟
    secs=secs%60; //余数秒

    m_durationTime=QString::asprintf ("%d:%d", mins,secs) ;
    ui->labRatio->setText (m_positionTime+"/"+m_durationTime) ;
}

void MediaPlayerVideo::onPositionChanged(qint64 position)
{
    //文件播放位置变化
    if (ui->sliderPosition->isSliderDown())
        return; // 如果正在拖动滑条，退出

    ui->sliderPosition->setSliderPosition(position);
    int secs=position/1000;//秒
    int  mins=secs/60; //分钟
    secs=secs%60;//余数秒

    m_positionTime=QString::asprintf ("%d:%d" ,mins, secs) ;
    ui->labRatio->setText(m_positionTime+" /"+m_durationTime);
}


void MediaPlayerVideo::on_btnOpenFile_clicked()
{
    //打开文件
    QString curPath=QDir::homePath() ; //获取系统当前目录
    QString dlgTitle="选择视频文件"; //对话框标题
    QString filter="wmv 文件(* .wmv) ; ;mp4文件(* .mp4); ;所有文件(*.*)";
    QString aFile=QFileDialog::getOpenFileName (this, dlgTitle, curPath, filter) ;
    if (aFile. isEmpty() )
        return;

    QFileInfo   fileInfo(aFile) ;
    ui->labCurMedia->setText (fileInfo.fileName() ) ;

    m_Player->setMedia (QUrl::fromLocalFile(aFile));//设置播放文件
    m_Player->play() ;
}

void MediaPlayerVideo::on_btnPlay_clicked()
{

    //播放视频
    m_Player->play();
}

void MediaPlayerVideo::on_btnPause_clicked()
{
    //暂停
    m_Player->pause();
}

void MediaPlayerVideo::on_btnStop_clicked()
{
    //停止
    m_Player->stop();
}

void MediaPlayerVideo::on_sliderVolumn_valueChanged(int value)
{
    //调节音量
    m_Player->setVolume(value);
}

void MediaPlayerVideo::on_btnSound_clicked()
{
    bool mute = m_Player->isMuted();

    m_Player->setMuted(mute);

    if (mute){
        ui->btnSound->setText("声音");
    }
    else
    {
        ui->btnSound->setText("静音");
    }

}

void MediaPlayerVideo::on_sliderPosition_valueChanged(int value)
{
    //播放位置
    m_Player->setPosition(value);


}

void MediaPlayerVideo::on_btnFullScreen_clicked()
{
    //全屏按钮
    ui->videoWidget->setFullScreen(true);
}
